package com.ff.xyh.front.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @Auther FF
 * @Date 2022年 11月 12日
 * @Time 23:52
 */

/**
 * controller作为生产者端 listener作为消费者端
 * 在config声明交换机 队列 需要时进行绑定 也可以在消费者添加@RabbitListener注解时绑定
 * 交换机名，队列名，routingKey均用类的静态最终变量声明
 *
 */

/**
 * 用于配置帖子操作相关的交换机和队列
 */
@Configuration
public class PostRabbitConfig {

    public static final String Exchange_Post = "post.exchange";
    public static final String Exchange_File = "file.exchange";
    public static final String Queue_PostImg = "postImg.queue";
    public static final String Queue_UploadFile = "uploadFile.queue";

    public static final String Routing_Key_PostImg = "postImg";
    public static final String Routing_Key_UploadFile = "uploadFile";

    public static final String Dead_Exchange_Post = "post.deadExchange";
    public static final String Dead_Queue_PostImg = "postImg.deadQueue";

    public static final String Dead_Exchange_File = "file.deadExchange";
    public static final String Dead_Queue_UploadFile = "uploadFile.deadQueue";


    /** 交换机绑定队列 一些队列可以绑定routingKey  */

    // 声明帖子交换机
    @Bean("exchangePost")
    public DirectExchange exchangePost() {
        return new DirectExchange(Exchange_Post);
    }
    // 声明文件交换机
    @Bean
    public DirectExchange exchangeFile() {
        return new DirectExchange(Exchange_File);
    }
    // 声明帖子死信交换机
    @Bean("deadExchange")
    public DirectExchange deadExchangePost() {
        return new DirectExchange(Dead_Exchange_Post);
    }
    // 声明文件死信交换机
    @Bean
    public DirectExchange deadExchangeFile() {
        return new DirectExchange(Dead_Exchange_File);
    }

    // 声明帖子照片队列
    @Bean("queuePostImg")
    public Queue queuePostImg() {
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", Dead_Exchange_Post);
        arguments.put("x-dead-letter-routing-key", Routing_Key_PostImg);
//        arguments.put("x-dead-letter-exchange");
        return QueueBuilder.durable(Queue_PostImg).withArguments(arguments).build();
    }
    // 声明文件上传队列
    @Bean
    public Queue queueUploadFile() {
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", Dead_Exchange_File);
        arguments.put("x-dead-letter-routing-key", Routing_Key_UploadFile);
//        arguments.put("x-dead-letter-exchange");
        return QueueBuilder.durable(Queue_UploadFile).withArguments(arguments).build();
    }


    // 声明帖子照片死信队列
    @Bean("deadQueuePostImg")
    public Queue deadQueuePostImg() {
        return new Queue(Dead_Queue_PostImg);
    }
    // 声明文件上传队列
    @Bean
    public Queue deadQueueUploadFile() {
        return new Queue(Dead_Queue_UploadFile);
    }

    // 绑定帖子照片队列 到 帖子交换机 和 帖子死信交换机
    @Bean
    public Binding PostImgBinding(
            @Qualifier("exchangePost") DirectExchange exchange,
            @Qualifier("queuePostImg") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with(Routing_Key_PostImg);
    }
    @Bean
    public Binding PostImgDeadBinding(
            @Qualifier("deadExchange") DirectExchange exchange,
            @Qualifier("deadQueuePostImg") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with(Routing_Key_UploadFile);
    }
    // 绑定文件上传队列 到 文件交换机 和 文件死信交换机
    @Bean
    public Binding UploadFileBinding(
            DirectExchange exchangeFile,
            Queue queueUploadFile) {
        return BindingBuilder.bind(queueUploadFile).to(exchangeFile).with(Routing_Key_PostImg);
    }
    @Bean
    public Binding UploadFileDeadBinding(
            DirectExchange deadExchangeFile,
            Queue queueUploadFile) {
        return BindingBuilder.bind(queueUploadFile).to(deadExchangeFile).with(Routing_Key_UploadFile);
    }

    /**
     * 添加自定义消息转换器 不需要声明 会自动注入
     * 原有消息转换器为序列化对象，性能差
     * 注意： 发送方与接收方需要使用相同的转换器
     * @return
     */
    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }
}
